//administrator
const router = require('koa-router')()
const jwt = require('jsonwebtoken');
const myKey = "zhangsan1234"

router.prefix('/administrator')

router.post('/login', async (ctx, next) => {
    let adminename = ctx.request.body.adminename;
    let adminepwd = ctx.request.body.adminepwd;
    let sql = 'SELECT * FROM administrator WHERE adminename=? AND adminepwd=?';
    let result = await ctx.db.EXCUTE(sql, [adminename, adminepwd]);
    console.log(result);
    if (result[0]) {
        ctx.body = {
            code: 200,
            data: result,
            msg: 'success'
        }
    } else {
        ctx.body = {
            code: 200,
            data: result,
            msg: '登录失败'
        }
    }
})
router.post('/getall', async (ctx, next) => {
    let token = ctx.request.body.token;
    // console.log();
    console.log(token);
    // 解密
    let result0 = await ctx.db.EXCUTE('select * from administrator');
    jwt.verify(token, myKey)
    let result = jwt.verify(token, myKey)
    for (let i = 0; i < result0.length; i++) {
        if (result.username == result0[i].adminename) {  //判断token的有效性
            console.log(result0[i].istop);
            ctx.body = {
                code: 200,
                msg: 'token有效',
                state: true,
                data: result0,
                istop: result0[i].istop
            }
            return 
        }
    }
    ctx.body = {
        code: 200,
        msg: 'token无效',
        state: false
    }
})
router.post('/addadmin', async (ctx, next) => {
    let body = ctx.request.body.data;

    console.log(body);
    let result0 = await ctx.db.EXCUTE('insert into administrator (adminename, adminepwd, istop) values(?, ?, ?)',[body.adminename, body.adminepwd, body.istop]);
    ctx.body = {
        code: 200
    }
})
router.post('/edit', async (ctx, next) => {
    let body = ctx.request.body.form;
    console.log(body);
    let result0 = await ctx.db.EXCUTE('update administrator set adminename=?, adminepwd=?, istop=? where admineid=?',[body.adminename, body.adminepwd, body.istop, body.admineid]);
    ctx.body = {
        code: 200
    }
})


router.post('/myself', async (ctx, next) => {
    let token = ctx.request.body.token;
    let result0 = await ctx.db.EXCUTE('select * from administrator ');
    jwt.verify(token, myKey)
    let result = jwt.verify(token, myKey)
    for (let i = 0; i < result0.length; i++) {
        if (result.username == result0[i].adminename) {  //判断token的有效性
            ctx.body = {
                code: 200,
                msg: 'token有效',
                state: true,
                data: result0[i]
            }
            return 
        }
    }
    ctx.body = {
        code: 200,
        msg: 'token无效',
        state: false
    }
})


router.post('/editmyself', async (ctx, next) => {
    let token = ctx.request.body.token;
    let form = ctx.request.body.form;
    console.log(form);
    let result0 = await ctx.db.EXCUTE('select * from administrator');
    jwt.verify(token, myKey)
    let result = jwt.verify(token, myKey)
    for (let i = 0; i < result0.length; i++) {
        if (result.username == result0[i].adminename) {  //判断token的有效性
            let sql = "update administrator set adminename=?, adminepwd=? where admineid=?"
            let result = await ctx.db.EXCUTE(sql, [form.adminename, form.adminepwd, form.admineid])
            ctx.body = {
                code: 200,
                msg: 'token有效',
                state: true,
                data: result
            }
            return 
        }
    }
    ctx.body = {
        code: 200,
        msg: 'token无效',
        state: false
    }
})
module.exports = router